//CPKUNIT商品，如果成对提交，如果两个都有新零售，则进行新零售价进行梯度比较；
//如果只有其中一个有新零售，则另一个取当前生效零售值进行价格梯度比较。价格梯度错误时，
//则错误提示：CPK&UNIT价格梯度错误
var result_50 = JSON.parse('{"CODE":0, "ACTION":"","LEVEL":"", "MSG":"","DATA":"","RULE":"50"}');
var CPK_UNIT_LIST = !isNull(JSON_CPK_UNIT) ? JSON.parse(JSON_CPK_UNIT) : [];
var JSON_ITEM_LIST = !isNull(ITEM_LIST) ? JSON.parse(ITEM_LIST) : [];
var RETAIL;
var CASE_ITEM_QTY;
var BPrice;
var SPrice;

function MathRound(number) {
  return Math.round(number * 100) / 100;
};

//如果item在item list中取new retail价格
function findNewRetail(ITEM, JSON_ITEM_LIST, CPK_UNIT_LIST) {
  for (var i = 0; i < JSON_ITEM_LIST.length; i++) {
    if (ITEM == JSON_ITEM_LIST[i].ITEM_NBR && !isNull(JSON_ITEM_LIST[i].NEW_RETAIL)) {
      RETAIL = JSON_ITEM_LIST[i].NEW_RETAIL / findCaseItemQTY(ITEM, CPK_UNIT_LIST);
      return RETAIL;
    } else {
      RETAIL = findNewRetailInUnitList(ITEM, CPK_UNIT_LIST);
    }

  }
  return RETAIL;
}

function CheckCPKUNIT(ITEM_NBR, CPK_UNIT_LIST, ITEM_LIST) {
  for (var i = 0; i < CPK_UNIT_LIST.length; i++) {
    for (var j = i + 1; j < CPK_UNIT_LIST.length - 1; j++) {
      //当大包装item状态为A，取大包装价格，当状态不为A取下一个小包装的价格
      if (CPK_UNIT_LIST[i].CASE_ITEM_STATUS == 'A') {
        BPrice = isNull(findNewRetail(CPK_UNIT_LIST[i].CASE_ITEM_NBR, ITEM_LIST, CPK_UNIT_LIST)) ? CPK_UNIT_LIST[i].CASE_ITEM_NBR_RTL_AMT / CPK_UNIT_LIST[i].CASE_ITEM_QTY : findNewRetail(CPK_UNIT_LIST[i].CASE_ITEM_NBR, ITEM_LIST, CPK_UNIT_LIST);
      } else {
        BPrice = isNull(findNewRetail(CPK_UNIT_LIST[j].CASE_ITEM_NBR, ITEM_LIST, CPK_UNIT_LIST)) ? CPK_UNIT_LIST[j].CASE_ITEM_NBR_RTL_AMT / CPK_UNIT_LIST[j].CASE_ITEM_QTY : findNewRetail(CPK_UNIT_LIST[j].CASE_ITEM_NBR, ITEM_LIST, CPK_UNIT_LIST);
      }
      //小包装
      if (CPK_UNIT_LIST[j].CASE_ITEM_STATUS == 'A') {
        SPrice = isNull(findNewRetail(CPK_UNIT_LIST[j].CASE_ITEM_NBR, ITEM_LIST, CPK_UNIT_LIST)) ? CPK_UNIT_LIST[j].CASE_ITEM_NBR_RTL_AMT / CPK_UNIT_LIST[j].CASE_ITEM_QTY : findNewRetail(CPK_UNIT_LIST[j].CASE_ITEM_NBR, ITEM_LIST, CPK_UNIT_LIST);
      } else {
        SPrice = BPrice;
      }
      if (
        //不同包装箱数
        CPK_UNIT_LIST[i].CASE_ITEM_QTY != CPK_UNIT_LIST[j].CASE_ITEM_QTY &&
        //大包装比小包装的便宜	 
        MathRound(BPrice) <= MathRound(SPrice) &&
        //比单只便宜			
        MathRound(SPrice) <= MathRound(SINGLE_NEW_RETAIL)) {
        continue;
      } else {
        return false;
      }
    }
    return true;
  }
};



//如果item不在item list中，在JSON_CPK_UNIT中取价格
function findNewRetailInUnitList(ITEM, UNIT_LIST) {
  if (ITEM == UNIT_LIST[0].SINGLE_ITEM_NBR) {
    return UNIT_LIST[0].SINGLE_ITEM_NBR_RTL_AMT
  } else {
    for (var i = 0; i < UNIT_LIST.length; i++) {
      if (ITEM == UNIT_LIST[i].CASE_ITEM_NBR) {
        return UNIT_LIST[i].CASE_ITEM_NBR_RTL_AMT / UNIT_LIST[i].CASE_ITEM_QTY
      }
    }
    return 0;
  }
}

//取箱包数
function findCaseItemQTY(ITEM, CPK_UNIT_LIST) {
  for (var i = 0; i < CPK_UNIT_LIST.length; i++) {
    if (ITEM == CPK_UNIT_LIST[i].CASE_ITEM_NBR) {
      return CPK_UNIT_LIST[i].CASE_ITEM_QTY
    }
    if (ITEM == CPK_UNIT_LIST[i].SINGLE_ITEM_NBR) {
      return 1;
    }
  }
}

//是CPKUNIT商品
if (!isNull(JSON_CPK_UNIT) && !isNull(NEW_RETAIL) && ITEM_STATUS_CODE == 'A') {
  if (CPK_UNIT_LIST[0].SINGLE_ITEM_STATUS == 'A') {
    var SINGLE_NEW_RETAIL = !isNull(findNewRetail(CPK_UNIT_LIST[0].SINGLE_ITEM_NBR, JSON_ITEM_LIST, CPK_UNIT_LIST)) ? findNewRetail(CPK_UNIT_LIST[0].SINGLE_ITEM_NBR, JSON_ITEM_LIST, CPK_UNIT_LIST) : CPK_UNIT_LIST[0].SINGLE_ITEM_NBR_RTL_AMT;
  } else {
    var SINGLE_NEW_RETAIL = 999999;
  }
  if (CPK_UNIT_LIST[0].CASE_ITEM_STATUS == 'A') {
    var CASE_NEW_RETAIL = !isNull(findNewRetail(CPK_UNIT_LIST[0].CASE_ITEM_NBR, JSON_ITEM_LIST, CPK_UNIT_LIST)) ? findNewRetail(CPK_UNIT_LIST[0].CASE_ITEM_NBR, JSON_ITEM_LIST, CPK_UNIT_LIST) : CPK_UNIT_LIST[0].CASE_ITEM_NBR_RTL_AMT;
  } else {
    var CASE_NEW_RETAIL = 0;
  }
  if (CPK_UNIT_LIST.length == 1 && MathRound(CASE_NEW_RETAIL) > MathRound(SINGLE_NEW_RETAIL)) {
    result_50.CODE = 2;
    result_50.ACTION = "CHECKING_REPORT";
    result_50.MSG = "CPK&UNIT价格梯度错误,请提交成对的item并填写符合梯度的价格";

  }
  if (!CheckCPKUNIT(ITEM_NBR, CPK_UNIT_LIST, JSON_ITEM_LIST)) {
    result_50.CODE = 2;
    result_50.ACTION = "CHECKING_REPORT";
    result_50.MSG = "CPK&UNIT价格梯度错误,请提交成对的item并填写符合梯度的价格";
  }
}
JSON.stringify(result_50);

